package gui;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import model.Delivery;
import model.Invoice;
import database.CustomerOperations;
import database.EstateOperations;
import database.RoadOperations;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.sql.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Vector;

@SuppressWarnings("serial")
public class tab3 extends JPanel implements ItemListener,ActionListener,ListSelectionListener
{
	JPanel p1 ,p2;
	JButton print,all,estate,house2;
	JComboBox<String> est, road1,house;
	private String estName;
	private String roadName;
	private String houseName;
	private ArrayList<String> ests;
	private ArrayList<String> rds;
	private ArrayList<String> hse;
	private EstateOperations eo;
	private RoadOperations ro;
	private CustomerOperations co;
	public static JTable table;
	public static DefaultTableModel model;
	
	public tab3() 
	{
		this.setPreferredSize(new Dimension(795, 395));
		this.setLayout(new FlowLayout());
		eo=new EstateOperations();
		ro=new RoadOperations();
		co=new CustomerOperations();
		
		//Panel 1 buttons and combos
		p1 = new JPanel();
		p1.setLayout(new FlowLayout());
		p1.setPreferredSize(new Dimension(795, 32));
		
		//list of estates in combo box
		est = new JComboBox<String>();	
		est.setPreferredSize(new Dimension(100,25));
		ests=new ArrayList<String>();
		ests=eo.getEstateNames();
		for(int i=0;i<ests.size();i++)
		{
			est.addItem(ests.get(i));
		}
		est.addItemListener(this);
		estName=(String)est.getSelectedItem();
		
		//list of roads in combo box
		road1 = new JComboBox<String>();	
		road1.setPreferredSize(new Dimension(100,25));
		rds=new ArrayList<String>();
		rds=ro.getRoadNames(estName);
		for(int i=0;i<rds.size();i++)
		{
			road1.addItem(rds.get(i));
		}
		road1.addItemListener(this);
		roadName=(String)road1.getSelectedItem();		
		
		//list of houses in combo box
		house = new JComboBox<String>();	
		house.setPreferredSize(new Dimension(100,25));
		hse=new ArrayList<String>();
		hse=co.getHouseNames(roadName,co.getEstId(estName));
		for(int i=0;i<hse.size();i++)
		{
			house.addItem(hse.get(i));
		}
		house.addItemListener(this);
		houseName=(String)house.getSelectedItem();
		
		print = new JButton("PRINT");
		print.addActionListener(this);
		all = new JButton("ALL");
		all.addActionListener(this);
		estate = new JButton("ESTATE");
		estate.addActionListener(this);
		house2 = new JButton("HOUSE");
		house2.addActionListener(this);
		
		
		p1.add(est);
		p1.add(road1);
		p1.add(house);
		p1.add(Box.createRigidArea(new Dimension(50,0)));
		p1.add(all);
		p1.add(estate);
		p1.add(house2);
		p1.add(print);
		
		//P2
		p2 = new JPanel();
		p2.setPreferredSize(new Dimension(800, 260));
		
		// create blank JTable on screen
		String[] cols={"HOUSE","ESTATE","ROAD","AMOUNT"};
		int numRows=1000;
		model=new DefaultTableModel(numRows,cols.length);
		model.setColumnIdentifiers(cols);
		table = new JTable(model);
		table.setFillsViewportHeight(true);
		table.setAutoCreateColumnsFromModel(false); 
		table.setRowSelectionAllowed(true); 
		ListSelectionModel selectionModel = table.getSelectionModel();  
        selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
        selectionModel.addListSelectionListener(this);  
		
		JScrollPane sp=new JScrollPane(table);
		sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		sp.setPreferredSize(new Dimension(790, 250));
				
		//adjust size of columns on table
		TableColumnModel tbm=table.getColumnModel();
		tbm.getColumn(0).setMaxWidth(80);
		tbm.getColumn(3).setMaxWidth(80);
		model.setRowCount(0);
		
		p2.add(sp,BorderLayout.CENTER);
		
		this.add(p1, BorderLayout.NORTH);
		this.add(p2, BorderLayout.SOUTH);
		
	}
	
	@Override
	public void itemStateChanged(ItemEvent ae) 
	{
			if (ae.getSource() == est && ae.getStateChange() == ItemEvent.SELECTED)
			{
				estName=(String)est.getSelectedItem();
				rds=ro.getRoadNames(estName);
				road1.removeAllItems();
				for(int i=0;i<rds.size();i++)
				{
					road1.addItem(rds.get(i));
				}
			}
			if (ae.getSource() == road1 && ae.getStateChange() == ItemEvent.SELECTED)
			{
				roadName=(String)road1.getSelectedItem();
				hse=co.getHouseNames(roadName,co.getEstId(estName));
				house.removeAllItems();
				for(int i=0;i<hse.size();i++)
				{
					house.addItem(hse.get(i));
				}
			}
			if (ae.getSource() == house && ae.getStateChange() == ItemEvent.SELECTED)
			{
				houseName=(String)house.getSelectedItem();
			}
	}

	//display orders in table
	public void displayAll()
	{
		ArrayList<Invoice> inv=new ArrayList<Invoice>();
		inv=eo.getAllDue();
		model.setRowCount(0);
		String e="";
		String r="";
		int counter=0;
		for(int i=0;i<inv.size();i++)
		{
				Vector<Object> row=new Vector<Object>();
				row.add(inv.get(i).getHouse());
				e=ro.getEstName(inv.get(i).getEst());
				row.add(e);
				r=ro.getRoadName(inv.get(i).getRoad());
				row.add(r);
				row.add(inv.get(i).getAmount());
				model.insertRow(counter,row);
				counter++;
		}
	}
	

	//display orders in table
	public void displayEst()
	{
		ArrayList<Invoice> inv=new ArrayList<Invoice>();
		inv=eo.getAllDue();
		model.setRowCount(0);
		String e="";
		String r="";
		int counter=0;
		for(int i=0;i<inv.size();i++)
		{
			if(ro.getEstName(inv.get(i).getEst()).equals(estName))
			{
				Vector<Object> row=new Vector<Object>();
				row.add(inv.get(i).getHouse());
				e=ro.getEstName(inv.get(i).getEst());
				row.add(e);
				r=ro.getRoadName(inv.get(i).getRoad());
				row.add(r);
				row.add(inv.get(i).getAmount());
				model.insertRow(counter,row);
				counter++;
			}
		}
	}
	

	//display orders in table
	public void displayHouse()
	{
		ArrayList<Invoice> inv=new ArrayList<Invoice>();
		inv=eo.getAllDue();
		model.setRowCount(0);
		String e="";
		String r="";
		int counter=0;
		for(int i=0;i<inv.size();i++)
		{
			if(ro.getEstName(inv.get(i).getEst()).equals(estName) &&
					inv.get(i).getHouse().equals(houseName)	&&
					ro.getRoadName(inv.get(i).getRoad()).equals(roadName))
			{
				Vector<Object> row=new Vector<Object>();
				row.add(inv.get(i).getHouse());
				e=ro.getEstName(inv.get(i).getEst());
				row.add(e);
				r=ro.getRoadName(inv.get(i).getRoad());
				row.add(r);
				row.add(inv.get(i).getAmount());
				model.insertRow(counter,row);
				counter++;

			}
		}
	}
	
	
	
	
	@Override
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource()==all)
		{
			displayAll();
		}
		if(ae.getSource()==estate)
		{
			displayEst();
		}
		if(ae.getSource()==house2)
		{
			displayHouse();
		}
		if(ae.getSource()==print)
		{
			try {  
				MessageFormat headerFormat = new MessageFormat("");
	              MessageFormat footerFormat = new MessageFormat("- {0} -");
	              table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
	            } catch (PrinterException pe) 
	            {
	              System.err.println("Error printing: " + pe.getMessage());
	            }
	
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent ae) 
	{
		System.out.println(table.getValueAt(table.getSelectedRow(), 0));
		
		
	}

}
